package com.jie.deletefile.Calendar;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.jie.deletefile.Permission.CheckPermission;
import com.jie.deletefile.Permission.PermissionActivity;
import com.jie.deletefile.R;

public class CalendarActivity extends AppCompatActivity implements View.OnClickListener {

    private android.widget.Button btncalendar;
    private android.widget.RelativeLayout activitycalendar;
    private static final int REQUEST_CODE = 1;
    private static final String[] PERSIMMION = new String[]{
            Manifest.permission.READ_CALENDAR,
            Manifest.permission.WRITE_CALENDAR
    };
    private CheckPermission checkPermission;

    private boolean hadPermission;
    private Button btn_rili;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        this.btncalendar = (Button) findViewById(R.id.btn_calendar);
        btn_rili = (Button) findViewById(R.id.btn_rili);
        btn_rili.setOnClickListener(this);
        btncalendar.setOnClickListener(this);
        checkPermission = new CheckPermission(this);
        if (checkPermission.permissionSet(PERSIMMION)) {
            startPermissionActivity();
        } else {
            hadPermission = true;
        }
    }

    @Override
    public void onClick(View v) {
        if (!hadPermission) {
            Toast.makeText(this, "缺少权限", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (v.getId()) {
            case R.id.btn_calendar:
                Intent intent = new Intent(CalendarActivity.this, DateActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_rili:
                Intent intent1 = new Intent(CalendarActivity.this, RiliActivity.class);
                startActivity(intent1);

        }

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void startPermissionActivity() {
        PermissionActivity.startActivityForResult(CalendarActivity.this, REQUEST_CODE, PERSIMMION);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && requestCode == PermissionActivity.PERMISSION_DENIEG) {
            hadPermission = false;
        } else {
            hadPermission = true;
        }
    }
}
